<script>
import { mapValues } from 'lodash-es';

export default {
  name: 'RuntimeNode',
  inject: ['runtime'],
  provide() {
    return {
      runtime: {
        ...this.runtime,
        node: this,
      },
    };
  },
  props: {
    template: {
      type: Object,
      required: true,
    },
  },
  render(h) {
    const { runtime, template } = this;
    const { model } = runtime.root;
    return h(runtime?.componentMap?.[template.type] ?? template.type, {
      props: mapValues(template.props, (value) =>
        typeof value === 'function' ? value(model) : value,
      ),
      on: mapValues(
        template.on,
        (value) =>
          (...args) =>
            value(args, model, this),
      ),
    });
  },
};
</script>
